package com.arron.juc;

import java.util.concurrent.CountDownLatch;

/**
 * @Classname CountDownLatchDemo
 * @Description TODO
 * @Date 2020/8/21 14:45
 * @Created by 刘
 */

public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
//        banzhangLock();

        /**
         * 秦朝灭6国，创建秦帝国，一统华夏
         */

        CountDownLatch countDownLatch = new CountDownLatch(6);

         for (int i = 1; i <= 6; i++) {
             new Thread(()->{
                 System.out.println(Thread.currentThread().getName()+"\t被灭");
                 countDownLatch.countDown();
             },EnumDemo.forEachIndex(i).getName()).start();
         }
        countDownLatch.await();
        System.out.println("*************秦国一统华夏");
    }

    /**
     * 6个同学，一个班长，班长必须等6个同学走完了之后才锁门
     */
    private static void banzhangLock() throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <= 6; i++) {
             new Thread(()->{
                 System.out.println(Thread.currentThread().getName()+"\t离开教室");
                 countDownLatch.countDown();
             },String.valueOf(i)).start();
         }
        //主线程等待6个线程走完之后再离开
        countDownLatch.await();

        System.out.println(Thread.currentThread().getName()+"\t最后离开教室");
    }

}
